package com.imelite.qyh.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class BaseDateUtil {
    public static final String DATE_FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";//默认日期格式
    public static final String DATE_FORMAT_DAY = "yyyy-MM-dd";


    public static String getDateDayStr(Date date, int day) {
        if (date == null) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String monthStr = year + "-" + month + "-" + day;
        return monthStr;
    }

    public static Date getDateDay(Date date, int day) {
        String str = getDateDayStr(date, day);
        Date d = getDateByFormatString(str, "yyyy-MM-dd");
        return d;
    }

    //获取某月天数
    public static int getDayOfMonth(int year, int month) {
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);
        int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);
        return day;
    }

    /**
     * @param date
     * @param field
     * @return
     */
    public static int getField(Date date, int field) {
        if (date == null) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return calendar.get(field);
    }

    /**
     * 判断 与当前时间是否相等
     *
     * @param date
     * @param field
     * @return
     */
    public static boolean isSame(Date date, int field) {
        int f1 = BaseDateUtil.getField(date, field);
        int f2 = BaseDateUtil.getField(new Date(), field);

        return f1 == f2;
    }

    /**
     * @param str
     * @return
     */
    public static Date getDateDay(String str) {
        return getDateByFormatString(str, DATE_FORMAT_DAY);
    }

    /**
     * @param request
     * @param paramName
     * @return
     */
    public static Date getDateSecond(HttpServletRequest request, String paramName) {
        String paramValue = request.getParameter(paramName);
        return getDateSecond(paramValue);
    }

    /**
     * @param str
     * @return
     */
    public static Date getDateSecond(String str) {
        return getDateByFormatString(str, DATE_FORMAT_SECOND);
    }

    /**
     * @param date
     * @param field
     * @param value
     */
    public static Date setField(Date date, int field, int value) {
        if (date == null) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.set(field, value);

        return calendar.getTime();
    }

    /**
     * @param date
     * @param fmt
     * @return
     */
    public static Date getDay(Date date, String fmt) {
        String dateStr = getFormatString(date, fmt);
        Date date2 = getDateByFormatString(dateStr, fmt);

        return date2;
    }

    /**
     * @param date
     * @return
     */
    public static String getFormatStringDay(Date date) {
        return getFormatString(date, DATE_FORMAT_DAY);
    }

    /**
     * @param date
     * @return
     */
    public static String getFormatStringSecond(Date date) {
        return getFormatString(date, DATE_FORMAT_SECOND);
    }

    /**
     * 日期 按格式 输出成字符串
     *
     * @param date
     * @param fmt
     * @return
     */
    public static String getFormatString(Date date, String fmt) {
        if (date == null) {
            return "- -";
        }
        if (fmt == null || fmt.trim().equals("")) {
            fmt = DATE_FORMAT_SECOND;
        }

        DateFormat dateFormat = new SimpleDateFormat(fmt);
        return dateFormat.format(date);
    }

    /**
     * 将字符串转成时间
     *
     * @param dateStr
     * @param fmt
     * @return
     */
    public static Date getDateByFormatString(String dateStr, String fmt) {
        if (BaseStringUtil.isBlank(dateStr)) {
            return new Date();
        }
        if (BaseStringUtil.isBlank(fmt)) {
            fmt = DATE_FORMAT_SECOND;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    /**
     * 得到日期
     *
     * @param obj
     * @return
     */
    public static Date getDateByFormatString(Object obj) {
        if (obj instanceof Date) {
            return (Date) obj;
        }
        if (obj instanceof String) {
            return BaseDateUtil.getDateByFormatString((String) obj, "");
        }
        return new Date();
    }

    /**
     * 得到 date 之后 day天 时间
     *
     * @param date ：null表示当前时间
     * @param day  : 负数表示 之前
     * @return
     */
    public static Date addDay(Date date, int day) {
        return addFieldValue(date, Calendar.DATE, day);
    }

    /**
     * @param date
     * @param minute
     * @return
     */
    public static Date addMinute(Date date, int minute) {
        return addFieldValue(date, Calendar.MINUTE, minute);
    }

    /**
     * @param date
     * @param field
     * @param value
     * @return
     */
    public static Date addFieldValue(Date date, int field, int value) {
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        calendar.set(field, calendar.get(field) + value);
        return calendar.getTime();
    }

    /**
     * 得到两个时间 之间 的 一个 随机 时间
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Date getDateRandom(Date beginDate, Date endDate) {
        if (beginDate == null) {
            beginDate = new Date();
        }
        if (endDate == null) {
            endDate = new Date();
        }

        long begin = beginDate.getTime();
        long end = endDate.getTime();

        long time = begin + (long) (Math.random() * (end - begin));

        return new Date(time);
    }

    /**
     * 伪造  经常更新 假象
     *
     * @param createTime
     * @return
     */
    public static String getDiffDay(Date createTime) {
        if (createTime == null) {
            return "刚刚";
        }
        int diffDay = getDiffDay(createTime, new Date());
        diffDay = diffDay % 365 % 30;
        return diffDay + "天前";
    }

    /**
     * 得到两个日期相差 几天
     *
     * @param beginDate
     * @param endDate
     * @return int
     */
    public static int getDiffYear(Date beginDate, Date endDate) {
        int diffDay = getDiffDay(beginDate, endDate);
        return diffDay / 365;
    }

    /**
     * 得到两个日期相差 几个月
     *
     * @param beginDate
     * @param endDate
     * @return int
     */
    public static int getDiffMonth(Date beginDate, Date endDate) {
        int diffDay = getDiffDay(beginDate, endDate);
        return diffDay / 30;
    }

    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 == null) {
            return false;
        }
        if (date2 == null) {
            return false;
        }

        String str1 = BaseDateUtil.getFormatStringDay(date1);
        String str2 = BaseDateUtil.getFormatStringDay(date2);

        if (str1.equals(str2)) {
            return true;
        }
        return false;
    }

    /**
     * 得到两个日期相差 几天
     *
     * @param beginDate
     * @param endDate
     * @return int
     */
    public static int getDiffDay(Date beginDate, Date endDate) {
        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();

        if (beginDate != null) {
            beginCalendar.setTime(beginDate);
        }

        if (endDate != null) {
            endCalendar.setTime(endDate);
        }

        int beginYear = beginCalendar.get(Calendar.YEAR);
        int endYear = endCalendar.get(Calendar.YEAR);

        int beginDay = beginCalendar.get(Calendar.DAY_OF_YEAR);
        int endDay = endCalendar.get(Calendar.DAY_OF_YEAR);

        return (endDay - beginDay) + (endYear - beginYear) * 365;
    }

    /**
     * @param beginDate
     * @param endDate
     * @return
     */
    public static int getDiffHour(Date beginDate, Date endDate) {
        long diff = getDiffMillisecond(beginDate, endDate);
        int hour = (int) (diff / (1000 * 60 * 60));
        return hour;
    }

    /**
     * @param beginDate
     * @param endDate
     * @return
     */
    public static int getDiffMinute(Date beginDate, Date endDate) {
        long diff = getDiffMillisecond(beginDate, endDate);
        int minute = (int) (diff / (1000 * 60));
        return minute;
    }

    /**
     * 得到两个时间 相差 多少毫秒
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getDiffMillisecond(Date beginDate, Date endDate) {
        if (beginDate == null) {
            beginDate = new Date();
        }
        if (endDate == null) {
            endDate = new Date();
        }
        return endDate.getTime() - beginDate.getTime();
    }

    /**
     * 判断 当前时间 是否在 beginTime与endTime之间 即当前时间是否失效
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static boolean validDateBetween(Date beginDate, Date endDate) {
        if (beginDate == null || endDate == null) {
            return true;
        }
        Calendar sysdate = Calendar.getInstance();
        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();

        beginCalendar.setTime(beginDate);
        endCalendar.setTime(endDate);

        if (sysdate.after(beginCalendar) && sysdate.before(endCalendar)) {
            return true;
        }
        return false;
    }

    /**
     * 判断当前时间是否在 time 之前
     *
     * @param time
     * @return
     */
    public static boolean validateDateBefore(Date time) {
        return validateDateBefore(time, new Date());
    }

    /**
     * @param time1
     * @param timeStr2
     * @return
     */
    public static boolean validateDateBefore(Date time1, String timeStr2) {
        return validateDateBefore(time1, timeStr2, "yyyy-MM-dd");
    }

    /**
     * @param time1
     * @param timeStr2
     * @param format
     * @return
     */
    public static boolean validateDateBefore(Date time1, String timeStr2, String format) {
        Date time2 = BaseDateUtil.getDateByFormatString(timeStr2, format);
        return validateDateBefore(time1, time2);
    }

    /**
     * @param time1
     * @param time2
     * @return
     */
    public static boolean validateDateBefore(Date time1, Date time2) {
        if (time1 == null || time2 == null) {
            return false;
        }

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(time1);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(time2);

        return calendar1.before(calendar2);
    }

    /**
     * 2个时间是否相连接
     *
     * @param time1
     * @param time2
     * @param type
     * @return
     */
    public static Date validateDateBeforeReturnDate(Date time1, Date time2, String type) {
        if (time1 == null || time2 == null || type == null) {
            return null;
        }

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(time1);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(time2);

        switch (type) {
            case "Before":
                if (calendar1.before(calendar2)) {
                    return time1;
                }
                return time2;
            case "After":
                if (calendar1.before(calendar2)) {
                    return time2;
                }
                return time1;
            default:
                return null;
        }
    }

    /**
     * 判断是否有交集
     *
     * @return
     */
    public static boolean validateIntersection(Date sourceStart, Date sourceEnd, Date targetStart, Date targetEnd) {
        Date timePoint = getDateByFormatString("1900-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date a = sourceStart;
        Date b = sourceEnd;
        Date c = targetStart;
        Date d = targetEnd;
        if (getDiffMillisecond(sourceStart, targetStart) < 0) {
            a = targetStart;
            b = targetEnd;
            c = sourceStart;
            d = sourceEnd;
        }
        long aa = getDiffMillisecond(timePoint, a);
        long bb = getDiffMillisecond(timePoint, c);
        long cc = getDiffMillisecond(a, b);
        long dd = aa + cc;
        if (dd >= bb) {
            return true;
        }
        return false;
    }

    /**
     * @param time1
     * @param time2
     * @return
     */
    public static boolean validateDateBeforeEq(Date time1, Date time2) {
        return !validateDateBefore(time2, time1);
    }

    /**
     * @return
     */
    public static String getWeek() {
        int week = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);
        DecimalFormat decimalFormat = new DecimalFormat("00");
        return decimalFormat.format(week);
    }

    private static String[] weekDaysName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    private static int[] weekDaysCode = {7, 1, 2, 3, 4, 5, 6};

    /**
     * 获取星期
     *
     * @param date
     * @return
     */
    public static String getWeekNameOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[intWeek];
    }

    /**
     * 获取星期
     *
     * @param date
     * @return
     */
    public static int getWeekCodeOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysCode[intWeek];
    }

    /**
     * @param dateStr
     * @return
     */
    public static Date getMondayDate(String dateStr) {
        Date date = getDateByFormatString(dateStr, DATE_FORMAT_DAY);
        return getMondayDate(date);
    }

    /**
     * @param date
     * @return
     */
    public static Date getMondayDate(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }
        //设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前日期是一个星期的第几天
        int day = calendar.get(Calendar.DAY_OF_WEEK);

        //根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek() - day);

        return calendar.getTime();
    }

    /**
     * 判断2个时间段是否有交接
     *
     * @param beginTime1
     * @param endTime1
     * @param beginTime2
     * @param endTime2
     * @return
     */
    public static boolean isMixed(Date beginTime1, Date endTime1, Date beginTime2, Date endTime2) {
        if (getDiffMinute(beginTime1, beginTime2) == 0) {
            return true;
        }
//        if(getDiffMinute(endTime1,beginTime2) == 0){
//            return true;
//        }

        // beginTime1 在 beginTime2 ，endTime2之间
        if (validateDateBefore(beginTime2, beginTime1) && validateDateBefore(beginTime1, endTime2)) {
            return true;
        }

        //beginTime2  在  beginTime1 ，endTime1 之间
        if (validateDateBefore(beginTime1, beginTime2) && validateDateBefore(beginTime2, endTime1)) {
            return true;
        }

        //  2 在  1  中
        if (validateDateBefore(beginTime1, beginTime2) && validateDateBefore(endTime2, endTime1)) {
            return true;
        }

        //  1 在  2  中
        if (validateDateBefore(beginTime2, beginTime1) && validateDateBefore(endTime1, endTime2)) {
            return true;
        }

        return false;
    }

    /**
     * 得到  date 是  第几个 30分钟
     *
     * @param date
     * @return
     */
    public static int getMinute30(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        int seq = (hour - 9) * 2 + (minute + 1) / 30;

        return seq;
    }

    /**
     * @param day
     * @param hm
     * @return
     */
    public static Date getDate(String day, String hm) {
        String str = day + " " + hm;
        Date date = getDateByFormatString(str, "yyyy-MM-dd HH:mm");

        int minute = getField(date, Calendar.MINUTE);
        if (minute < 30) {
            minute = 0;
        } else {
            minute = 30;
        }

        date = setField(date, Calendar.MINUTE, minute);

        return date;
    }

    public static boolean isChangeForMinute(Date timeBegin1, Date timeEnd1, Date timeBegin2, Date timeEnd2) {
        if (BaseDateUtil.getDiffMinute(timeBegin1, timeBegin2) != 0 ||
                BaseDateUtil.getDiffMinute(timeEnd1, timeEnd2) != 0) {
            return true;
        }
        return false;
    }

    /**
     * @return Date
     * @throws
     * @Title: getFirstDayOfMonth
     * @Description: 获取这个月的第一天
     */
    public static Date getFirstDayOfMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return getDateByFormatString(getFormatString(c.getTime(), "yyyy-MM-dd 00:00:00"));
    }

    /**
     * @return Date
     * @throws
     * @Title: getFirstDayOfMonth
     * @Description　获取这个月的最后一天
     */
    public static Date getLastDayOfMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 0);
        return getDateByFormatString(getFormatString(c.getTime(), "yyyy-MM-dd 23:59:59"));
    }

    public static Date dateFormat(Date target, int month) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(target);
        cl.add(Calendar.MONTH, month);
        return cl.getTime();
    }

    /**
     *
     * @param month
     * @return
     */
    public static String getMonthStr(int month){
        if(month>9){
            return month + "";
        }
        return "0" + month;
    }

    /**
     * 
     * @param monthStr
     * @return
     */
    public static String getPreMonth(String monthStr){
        Date date = BaseDateUtil.getDateByFormatString(monthStr,"yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//今天的日期
        calendar.set(Calendar.MONTH,calendar.get(Calendar.MONTH)-1);//让日期加1  

        date = calendar.getTime();
        
        return BaseDateUtil.getFormatStringDay(date);
    }
}
